*****************************************************************************
*			          Figure Context with CMP  	             				*
*																			*			
* Author: 			Valentina Gonzalez Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			August 9 2024   										*
* Version:			Stata 17 												*                                                                          
*																			*
*****************************************************************************
/*
This do-file:
- Creates Figure A5 using data from ISSP. 

Input:
- Data\CMP\MPDataset_MPDS2020a_stata14.dta

Output:
-  Figure A5: Number of Nationalist Parties in Elections [Figures\Nationalist.pdf]

*/

*Defining Directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

*Calling the data
use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", clear 

*******************************************************************************
* Preparing variables
*******************************************************************************
{
gen year = year(edate) 

keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

gen radright=1 if parfam==70
replace radright=0 if parfam>70 | parfam<70
}

*******************************************************************************
* Graphs
*******************************************************************************
* Graph style
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle set color dknavy
grstyle yesno draw_major_hgrid yes
grstyle yesno draw_major_ygrid yes
grstyle color major_grid gs8
grstyle linepattern major_grid dot
grstyle color ci_area gs12%50
 graph set window fontface "Georgia"
}
// Figure A5: Number of Nationalist Parties in Elections
{
sort year  // Sort the dataset by year
collapse (sum) radright, by(year)  // Collapse the data by summing the 'radright' variable for each year
tsset year  // Declare the dataset to be time-series data with 'year' as the time variable

rolling, window(4) saving(rolling_dataset, replace): egen ma_radright = total(radright)  // Calculate a 4-year moving average of the 'radright' variable and save the result in 'rolling_dataset.dta'

use "rolling_dataset.dta", clear  // Load the 'rolling_dataset.dta' file
line mean start if start > 1969, title("Number of Nationalist Parties with 4-Yr Moving Average") xtitle("Year") ytitle("Number of Nationalist Parties")  // Create a line graph of the 4-year moving average of nationalist parties starting from 1970
graph export "Figure\Nationalist.pdf", as(pdf) replace  // Export the graph to a PDF file and replace any existing file with the same name

* Drop the .dta file
erase "rolling_dataset.dta"  // Delete the 'rolling_dataset.dta' file from the directory

}
